<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.keywords" placeholder="券名称、会员名称、会员账号" clearable style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="">
		<el-date-picker
          v-model="dataForm.startTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="起始时间">
		</el-date-picker>
	  </el-form-item>
	  <el-form-item label="">
		<el-date-picker
          v-model="dataForm.endTime"
          type="datetime"
          value-format = "yyyy-MM-dd HH:mm:ss"
          placeholder="结束时间">
		</el-date-picker>
	  </el-form-item>
      <el-form-item label="券类型">
		<el-select v-model="dataForm.goodsType" placeholder="券类型">
		  <el-option label="全部" value="" ></el-option>
		  <el-option label="普通" value="0" ></el-option>
      <el-option label="抵扣券" value="1" ></el-option>
      <el-option label="体验券" value="2" ></el-option>
      <el-option label="兑换商品" value="3"></el-option>
		</el-select>
	  </el-form-item>
      <el-form-item label="商品状态">
		<el-select v-model="dataForm.goodsStatues" placeholder="商品状态">
		  <el-option label="全部" value=""></el-option>
		  <el-option label="未审核" value="0"></el-option>
      <el-option label="审核通过" value="1"></el-option>
      <el-option label="审核未通过" value="-1"></el-option>
		</el-select>
	  </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-badge :value="this.count" class="item">
          <el-button size="small" @click="unaudited()">未审核</el-button>
        </el-badge>
        <!-- <el-button v-if="isAuth('goods:shoppinggoods:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('goods:shoppinggoods:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!-- <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column> -->
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间"
        width="155px">
      </el-table-column>
      <el-table-column
        prop="goodsImg"
        header-align="center"
        align="center"
        label="券主图">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover">
            <img :src="'http://image.yilianbaihui.cn/consumer/goods/' + scope.row.goodsImg" style="max-width: 300px"/>
            <img slot="reference" :src="'http://image.yilianbaihui.cn/consumer/goods/' + scope.row.goodsImg" :alt="'http://image.yilianbaihui.cn/consumer/goods/' + scope.row.goodsImg" style="max-height: 30px;max-width: 80px">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        prop="goodsName"
        header-align="center"
        align="center"
        label="券名称"
        width="170px"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="typeId"
        header-align="center"
        align="center"
        label="券类型"
        width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.typeId == 0" type="success">普通</el-tag>
          <el-tag v-if="scope.row.typeId == 1" type="success">抵扣券</el-tag>
          <el-tag v-if="scope.row.typeId == 2" type="success">体验券</el-tag>
          <el-tag v-if="scope.row.typeId == 3" type="success">兑换商品</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="initialInventory"
        header-align="center"
        align="center"
        label="现有库存">
      </el-table-column>
      <el-table-column
        prop="goodsStatus"
        header-align="center"
        align="center"
        label="状态"
        width="100px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.goodsStatus == -1" type="success">禁售</el-tag>
          <el-tag v-if="scope.row.goodsStatus == 0" type="success">待审核</el-tag>
          <el-tag v-if="scope.row.goodsStatus == 1" type="success">审核通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="trueName"
        header-align="center"
        align="center"
        label="会员名称"
        width="150px"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="会员账号"
        width="160px"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="userType"
        header-align="center"
        align="center"
        label="会员类型"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="isSale"
        header-align="center"
        align="center"
        label="上下架"
        width="100px">
        <template slot-scope="scope">
          <el-switch
            style="margin-left: 10px;margin-bottom: 5px;"
            v-model="scope.row.isSale"
            active-value= "1"
            inactive-value= "0"
            @change="batch(scope.row.goodsId, scope.row.isSale)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        prop="saleTime"
        header-align="center"
        align="center"
        label="上架时间"
        width="155px">
      </el-table-column>
      <el-table-column
        prop="shelvesTime"
        header-align="center"
        align="center"
        label="下架时间"
        width="155px">
      </el-table-column>
      <el-table-column
        prop="goodsStatus"
        header-align="center"
        align="center"
        label="审核"
        width="180px">
        <template slot-scope="scope">
          <el-button size="small" type="warning" icon="check" @click="adopt(scope.row.goodsId, 1)" v-if="(scope.row.goodsStatus)== 0">通过</el-button>
          <el-button size="small" type="danger" icon="edit" @click="adopt(scope.row.goodsId, 2)" v-if="(scope.row.goodsStatus)== 0">不通过</el-button>
          <p v-if="scope.row.goodsStatus !=  0">已审核</p>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.goodsId)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './coupon-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          keywords: '',
          goodsType: '',
          goodsStatues: '',
          startTime: '',
          endTime: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        count: 0
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/coupon/queryCouponList'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keywords': this.dataForm.keywords,
            'goodsType': this.dataForm.goodsType,
            'goodsStatues': this.dataForm.goodsStatues,
            'startTime': this.dataForm.startTime,
            'endTime': this.dataForm.endTime
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data.data.list)
            this.dataList = data.data.list[0].list
            this.count = data.data.list[0].count
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 未审核的优惠券
      unaudited () {
        this.dataForm.goodsStatues = 0
        this.getDataList()
      },
      batch (id, val) {
        let li = {
          goodsId: '',
          isSale: ''
        }
        li.goodsId = id
        li.isSale = val
        this.$confirm(`确定对该商品上下架操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/coupon/modifyUpperAndLowerFrames'),
            method: 'post',
            data: this.$http.adornData(li, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 审核操作
      adopt (id, val) {
        let li = {
          goodsId: '',
          isSale: ''
        }
        li.goodsId = id
        li.isSale = val
        this.$confirm(`确定对该商品进行审核操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/coupon/modifyAdoptById'),
            method: 'post',
            data: this.$http.adornData(li, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
